package com.greedy.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * @Author: Yanxy
 * @Date: Created in:2023-06-11
 * Description: 游戏页面
 */
public class TankFrame extends Frame {

    /**
     * 坦克
     */
    Tank myTank = new Tank(200, 200, Dir.DOWN, this);

    /**
     * 子弹
     */
    Bullet bullet = new Bullet(300, 300, Dir.DOWN);

    /**
     * 画面大小
     */
    static final int GAME_WIDTH = 800, GAME_HEIGHT = 600;

    public TankFrame() throws HeadlessException {
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setResizable(false);
        setTitle("tank war");
        setVisible(true);

        this.addKeyListener(new MyKeyListener());

        addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {

            }

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

            @Override
            public void windowClosed(WindowEvent e) {

            }

            @Override
            public void windowIconified(WindowEvent e) {

            }

            @Override
            public void windowDeiconified(WindowEvent e) {

            }

            @Override
            public void windowActivated(WindowEvent e) {

            }

            @Override
            public void windowDeactivated(WindowEvent e) {

            }
        });
    }

    /**
     * 解决画面闪烁问题，原理：利用显存画好，直接一次性显示出来
     */
    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        print(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    /**
     * 重新加载页面时，产生操作
     *
     * @param g 画笔
     */
    @Override
    public void paint(Graphics g) {
        myTank.paint(g);
        bullet.paint(g);
    }


    /**
     * 键盘监听处理类
     */
    class MyKeyListener extends KeyAdapter {

        /**
         * ← 按下去的时候调用
         */
        Boolean bL = false;

        /**
         * ↑ 按下去的时候调用
         */
        Boolean bU = false;

        /**
         * → 按下去的时候调用
         */
        Boolean bR = false;

        /**
         * ↓ 按下去的时候调用
         */
        Boolean bD = false;


        /**
         * 按键按下去的时候调用
         *
         * @param e 事件
         */
        @Override
        public void keyPressed(KeyEvent e) {
            // 获取按键的值
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                case KeyEvent.VK_CONTROL:
                    myTank.fire();
                    break;
                default:
                    break;
            }

            setMainTankDir();

        }

        /**
         * 按键抬起来的时候调用
         *
         * @param e 事件
         */
        @Override
        public void keyReleased(KeyEvent e) {
            // 获取按键的值
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                default:
                    break;
            }

            setMainTankDir();

        }

        /**
         * 重置坦克方向
         */
        private void setMainTankDir() {
            // 如果四个方向都没有按下去，坦克就不动
            if (!bL && !bU && !bR && !bD) {
                myTank.setMoving(false);
            } else {
                myTank.setMoving(true);
                if (bL) {
                    myTank.setDir(Dir.LEFT);
                }
                if (bU) {
                    myTank.setDir(Dir.UP);
                }
                if (bR) {
                    myTank.setDir(Dir.RIGHT);
                }
                if (bD) {
                    myTank.setDir(Dir.DOWN);
                }
            }
        }

    }

}
